package com.example.asus.zyfypt_tea_307plx.fragments;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;


import com.example.asus.zyfypt_tea_307plx.R;
import com.example.asus.zyfypt_tea_307plx.activities.TwareDetailActivity;
import com.example.asus.zyfypt_tea_307plx.adapter.TwareAdapter;
import com.example.asus.zyfypt_tea_307plx.bean.TwareBean;
import com.example.asus.zyfypt_tea_307plx.iface.AllListeners;
import com.example.asus.zyfypt_tea_307plx.model.TwareModel;

import java.util.List;

public class Fragment2 extends BaseFragment {
    private List<TwareBean> list;
    private RecyclerView recyclerView;
    private LinearLayoutManager layoutManager;
    private TwareAdapter adapter;
    private int page=1;
    private int lastVisibleItemPosition;

    public Fragment2(){}
    private AllListeners<List<TwareBean>> listener = new AllListeners<List<TwareBean>>() {

        @Override
        public void onResponse(List<TwareBean> beanlist) {
            if(page==1)
            {
                list=beanlist;
            }
            else {
                list.removeAll(beanlist);
                list.addAll(beanlist);
            }
            adapter.setList(list);

        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(context, "失败："+msg, Toast.LENGTH_SHORT).show();
            System.out.println(msg);
        }
    };
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
//        context = getActivity();
        View view=inflater.inflate(R.layout.fragment2,container,false);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        System.out.println("--f2---"+getSessionId());
        initRecyclerView(view);
        TwareModel model=new TwareModel();
        model.getResultList("Tware",1,getSessionId(),listener);
    }
    private void initRecyclerView(View view) {
        recyclerView=view.findViewById(R.id.recyclerview);
        layoutManager=new LinearLayoutManager(context);
        recyclerView.setLayoutManager(layoutManager);
        //每个item如果是确定高度，设置此项提高性能
        recyclerView.setHasFixedSize(true);
        //实例化适配器
        adapter=new TwareAdapter(context);
        TwareAdapter.OnItemClickListener onItemClickListener= new TwareAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {     //进入Item
                Intent intent=new Intent(getActivity(), TwareDetailActivity.class);
                intent.putExtra("resid",list.get(position).getId());
                intent.putExtra("pdfattach",list.get(position).getPdfattach());//
                intent.putExtra("name",list.get(position).getName());

                startActivity(intent);
                Toast.makeText(context, "click position:"+position, Toast.LENGTH_SHORT).show();
            }
        };

        adapter.setOnItemClickListener(onItemClickListener);
        recyclerView.setAdapter(adapter);
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItemPosition + 1 == list.size())    {
                    page += 1;
                    //再次实例化ArticleModel，调用方法获取网络数据，请求新一页数据
                    TwareModel twareModel = new TwareModel();
                    twareModel.getResultList("Tware", page, getSessionId(), listener);
                }
            }
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();//滚动结束后将赋值为可见条目中最后一条位置
            }
        });

    }
}
